﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Archives.Filters;
using Archives.Models.Users;
using FluentValidation.Mvc;
using System.Web.Security;
using Archives.Common.Attributes;
using Archives.Models;

namespace Archives.Controllers
{
   
    public class UserController : BaseController
    {
        //
        // GET: /User/
        [Archives.Common.Attributes.AllowAnonymousAttribute]
        public ActionResult Index()
        {

           
            ArchivesContext context = new ArchivesContext();
            
            var role = context.Users
                        .Where(m => m.Username == "abc")
                        .Include(b => b.)
                        .FirstOrDefault();

            List<User> list = role.ToList<User>();

            return View();
        }

        [Archives.Common.Attributes.AllowAnonymousAttribute]
        public ActionResult Login()
        {
            // If user has already logged in, redirect to the secure area.
            if (Request.IsAuthenticated)
            {
                return RedirectToAction("Profile", "User");
            }
            else
            {
                return View();
            }
        }

        [Archives.Common.Attributes.AllowAnonymousAttribute]
        [HttpPost]
        public ActionResult Login([CustomizeValidator(Properties = "UserName,Password")] User users,
            string returnUrl)
        {
            // Verify the fields.
            if (ModelState.IsValid)
            {
                // Validate the user login.
                if (Membership.ValidateUser(users.Username, users.Password))
                {

                    ////Regist userid to session
                    //Session[Constants.USER_INFO] = db.Users.Where(u => u.Email.Equals(users.Email)).First();

                    // Create the authentication ticket.
                    FormsAuthentication.SetAuthCookie(users.Username, false);

                    // Redirect to the secure area.
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 &&
returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Login", "User");
                    }
                }
                else
                {
                    ModelState.AddModelError("loginFail", "The user name or password provided is incorrect.");
                }
            }

            return View(users);
        }

        public ActionResult Profile()
        {
            return View();
        }

        public ActionResult LogOff()
        {
            // Delete the user details from cache.
            System.Web.HttpContext.Current.Cache.Remove(User.Identity.Name);

            // Delete the authentication ticket and sign out.
            FormsAuthentication.SignOut();

            return RedirectToAction("Index", "Page");
        }
    }
}
